package com.mrent.web.controller.front;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.CartGoods;
import com.mrent.entity.Goods;
import com.mrent.service.CartGoodsService;
import com.mrent.service.GoodsService;


/**
 * 
 * @author ibm t42
 *
 */
@Controller
public class ShopCartController {
	
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private CartGoodsService cartGoodsService;
	
	/**
	 * 查看购物车.
	 * 
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("/cart")
	public String cart(HttpSession session,ModelMap model){
		
		String sessionId = session.getId();
		List<CartGoods> list = cartGoodsService.findBySession(sessionId);
		System.out.println("list size:" + list.size());
		BigDecimal totalPrice = new BigDecimal(0);
		for(CartGoods cartGoods:list){
			BigDecimal goodsAmout = new BigDecimal(cartGoods.getGoodsAmount());
			BigDecimal goodsPrice = cartGoods.getGoodsPrice();
			BigDecimal goodsTotalPrice = goodsPrice.multiply(goodsAmout);
			totalPrice = totalPrice.add(goodsTotalPrice);
		}
		/*Object cartobj = session.getAttribute(Constance.SESSION_CART_NAME);
		ShopCart cart;
		if(cartobj != null){
			cart = (ShopCart)cartobj;
		}else{
			cart = new ShopCart();
		}*/
		model.addAttribute("cart", list);
		model.addAttribute("cartPrice", totalPrice);
		return "front/cart/detail";
	}
	
	
	/**
	 * 添加购物对象到购物车.
	 * 
	 * @param goodsNo
	 * @param model
	 * @return
	 */
	@RequestMapping("/cart/add")
	public String add(HttpSession session,String goodsNo,ModelMap model){
		Goods dbgoods = goodsService.getGoods(goodsNo);
		String sessionId = session.getId();
		CartGoods cartGoods = cartGoodsService.getBySessionGoodsNo(sessionId, goodsNo);
		if(cartGoods != null){
			Integer amount = cartGoods.getGoodsAmount();
			amount = amount.intValue() + 1;
			cartGoods.setGoodsAmount(amount);
			cartGoodsService.updateCartGoods(cartGoods);
		}else{
			System.out.println("save cartGoods...");
			CartGoods cg = new CartGoods();
			cg.setGoodsAmount(new Integer(1));
			cg.setGoodsName(dbgoods.getGoodsName());
			cg.setGoodsNo(goodsNo);
			cg.setGoodsPrice(dbgoods.getShopPrice());
			cg.setSessionId(sessionId);
			cg.setCreateDate(new Date());
			cartGoodsService.saveCartGoods(cg);
		}
		return "redirect:/cart";
		/*Object cartobj = session.getAttribute(Constance.SESSION_CART_NAME);
		ShopCart cart;
		if(cartobj != null){
			cart = (ShopCart)cartobj;
			//购物车对象.
			GoodsCart goods = new GoodsCart();
			goods.setGoodsNo(goodsNo);
			goods.setGoodsName(dbgoods.getGoodsName());
			goods.setGoodsAmount(new Integer(1));
			goods.setGoodsPrice(dbgoods.getShopPrice());
			//添加购物对象到购物车.
			cart.addGoods(goods);
		}else{
			cart = new ShopCart();
			//购物车对象.
			GoodsCart goods = new GoodsCart();
			goods.setGoodsNo(goodsNo);
			goods.setGoodsName(dbgoods.getGoodsName());
			goods.setGoodsAmount(new Integer(1));
			goods.setGoodsPrice(dbgoods.getShopPrice());
			//添加购物对象到购物车.
			cart.addGoods(goods);
			session.setAttribute(Constance.SESSION_CART_NAME, cart);
		}*/
		
		
		/*model.addAttribute("cart", cart.getShopCart());
		model.addAttribute("cartPrice", cart.getCartPrice());
		return "front/cart/detail";*/
		
	}

}
